package com.DeliveryProgram.www.servlet;

import com.DeliveryProgram.www.constant.BuScreen;
import com.DeliveryProgram.www.constant.Screen;
import com.DeliveryProgram.www.dao.BusLoad;
import com.DeliveryProgram.www.dao.BusSave;
import com.DeliveryProgram.www.po.Business;
import com.DeliveryProgram.www.po.Comment;
import com.DeliveryProgram.www.po.Coupons;
import com.DeliveryProgram.www.po.Order;
import com.DeliveryProgram.www.util.Scanners;
import com.DeliveryProgram.www.util.Screens;

import java.util.List;

/**商户系统*/
public class Shop {

    /**判断用户输入是否非法的变量*/
    private static boolean bool;

    /**用户主要操作的商户系统*/
    public static void business(Business bu){
        while(true) {
            /**打开商家版界面UI*/
            Screen.Business();
            switch (Scanners.getChoice()){

                /**查看商户的基本信息*/
                case 1:{
                    Screens.clear();
                    BuScreen.showMessage(bu);
                   bool=true;
                   while(bool) {
                       switch (Scanners.getChoice()) {
                           /**修改店名*/
                           case 1:{
                               Screens.clear();
                                changeShopname(bu);
                                /**完成系统操作返回主界面*/
                                bool=false;
                               break;
                           }
                           /**修改简介*/
                           case 2:
                               Screens.clear();
                               changeIntroduce(bu);
                               /**完成系统操作返回主界面*/
                               bool=false;
                               break;
                           /**退出查看*/
                           case 3:{
                               bool=false;
                               Screens.clear();
                               break;
                           }
                           default:{
                               System.out.println("请输入正确的选项");
                               /**让用户确认*/
                               Scanners.pause();
                               Screens.clear();
                               break;}
                       }
                   }
                    break;
                }
                /**查看订单*/
                case 2:{
                    Screens.clear();
                    doOrders(bu);
                    break;
                }
                /**查看评论*/
                case 3:{
                   showComment(bu);
                    break;
                }
                /**管理菜单*/
                case 4:{
                    showMenu(bu);
                    break;
                }
                /**管理折扣*/
                case 5: {
                    showCoupons(bu);
                    break;
                }
                /**退出*/
                case 0:{
                    System.out.println("感谢您的使用");
                    Scanners.pause();
                    return;
                }
                /**防止用户非法操作*/
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }

    }

    /**用户修改店名的系统*/
    public static void changeShopname(Business bu){
        bool=true;

        while(bool){
            System.out.println("请输入修改后的店名");
            String newName=Scanners.getScanner();
            System.out.println("是否为"+newName);
            System.out.println("1-是 2-否 3-放弃修改并退出");
            switch (Scanners.getChoice()){
                case 1:{
                    /**对商户对象中的店名进行修改*/
                    bu.setShopname(newName);
                    /**在数据库中对对应数据修改*/
                    BusSave.saveName(bu);
                    return;
                }
                case 2:{
                    Screens.clear();
                    break;
                }
                case 3:{
                    Screens.clear();
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**用户修改店铺简介*/
    public static void changeIntroduce(Business bu){
        bool=true;

        while(bool){
            System.out.println("请输入修改后的简介（少于30字）");
            String newIntroduce=Scanners.getScanner();
            System.out.println("是否为"+newIntroduce);
            System.out.println("1-是 2-否 3-放弃修改并退出");
            switch (Scanners.getChoice()){
                case 1:{
                    /**对商户对象中的店名进行修改*/
                    bu.setIndroduce(newIntroduce);
                    /**在数据库中对对应数据修改*/
                    BusSave.saveIntroduce(bu);
                    return;
                }
                case 2:{
                    Screens.clear();
                    break;
                }
                case 3:{
                    Screens.clear();
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**订单主系统*/
    public static void doOrders(Business bu){
        while(true){
            int choice=0;
            /**防止用户输入不存在的订单*/
            do {
                BuScreen.showOrders(bu);
                choice = Scanners.getChoice();
                if (choice-2 > bu.getOrders().size()) {
                    System.out.println("请输入存在的订单");
                    Scanners.pause();
                    Screens.clear();
                }
            } while (choice-2 > bu.getOrders().size());
            switch (choice) {
                /**输入0离开订单查询*/
                    case 0: return;
                    default: {
                    /**获取本来订单编号*/
                        choice -= 1;
                        checkOrder(bu, choice);
                        break;
                    }
            }
        }
    }

    /**处理单一订单*/
    public static void checkOrder(Business bu, int num){
        while(true){
            BuScreen.showOrder(bu,num);
            switch (Scanners.getChoice()){
                /**离开返回上一页*/
                case 0:return;
                /**修改订单的状态*/
                case 1:{
                    changeState(bu,num);
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**修改订单的状态*/
    public static void changeState(Business bu,int num){
        int old=bu.getOrders().get(num).getIsComplete();

        switch(old){
            case 1:{
                while(true){
                    System.out.println("该订单是否接受");
                    System.out.println("1-是   2-否");
                    System.out.println("------------");
                    switch (Scanners.getChoice()){
                        case 1:{
                            BusSave.changeState(bu,num,2);
                            return;
                        }
                        case 2:{
                            System.out.println("订单未接受无需修改");
                            Scanners.pause();
                            return;
                        }
                        default:{
                            System.out.println("请输入正确的选项");
                            /**让用户确认*/
                            Scanners.pause();
                            Screens.clear();
                            break;
                        }
                    }
                }
            }
            case 2:{
                while(true){
                    System.out.println("该订单是否完成");
                    System.out.println("1-是   2-否");
                    System.out.println("------------");
                    switch (Scanners.getChoice()){
                        case 1:{
                            BusSave.changeState(bu,num,3);
                            bu.addEarn(bu.getOrders().get(num).getMoney());
                            return;
                        }
                        case 2:{
                            System.out.println("订单未完成无需修改");
                            Scanners.pause();
                            return;
                        }
                        default:{
                            System.out.println("请输入正确的选项");
                            /**让用户确认*/
                            Scanners.pause();
                            Screens.clear();
                            break;
                        }
                    }
                }

            }
            case 3:{
                System.out.println("该订单已完成无需修改");
                Scanners.pause();
                return;
            }
            default:break;
        }


    }

    /**查看评论系统*/
    public  static void showComment(Business bu){
        while(true){
            Screens.clear();
            BuScreen.showComment(bu);
            switch (Scanners.getChoice()){
                case 0:{
                    Screens.clear();
                    return;
                }
                case 1:{
                    addComment(bu);
                    break;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**添加新的评论*/
    public static void addComment(Business bu){
        System.out.println("请输入你的评论");
        String comment=Scanners.getScanner();
        BusSave.addComment(comment);
        BusLoad.getNewComment(comment,bu);
        BusSave.addRelationship(bu);

    }

    /**菜单主系统*/
    public static void showMenu(Business bu){
        while(true){
            Screens.clear();
            BuScreen.showMenu(bu);
            System.out.println("---------------------------------------");
            System.out.println("1-添加菜单 2-修改菜单 0-退出");

            switch (Scanners.getChoice()){
                case 0:{
                    Screens.clear();
                    return;
                }
                case 1:{
                    Screens.clear();
                    addMenu(bu);
                    break;
                }
                case 2:{
                    Screens.clear();
                    changeMenus(bu);
                    break;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**添加菜单*/
    public static void addMenu(Business bu){
        System.out.println("请输入新菜名称");
        String name=Scanners.getScanner();
        System.out.println("请输入简介");
        String introduce=Scanners.getScanner();
        System.out.println("请输入价格");
        double price=Scanners.getChoice();
        BusSave.addNewMenu(bu,name,introduce,price);
        BusLoad.getNewId(bu);
    }

    /**修改菜单*/
    public  static void changeMenus(Business bu){
       while(true){
        Screens.clear();
        BuScreen.showMenu(bu);
        System.out.println("---------------------------------------");
        System.out.println("0-退出 |  输入需要修改的菜单");
           int choice=0;

           /**防止用户输入不存在的菜单*/
           do{
               choice = Scanners.getChoice();
               if(choice-2>bu.getOrders().size()){
                   System.out.println("请输入存在的菜单");
                   Scanners.pause();
                   Screens.clear();
                   BuScreen.showMenu(bu);
               }
           }while(choice-2>bu.getOrders().size());
        switch (choice){
            case 0:return;
            default:{
                changMenu(bu,choice-1);
                break;
            }
        }
       }
    }

    /**对某一菜单进行修改*/
    public static void changMenu(Business bu,int num) {
        while (true) {
            System.out.println(bu.getMenu().get(num).getName() + " " + bu.getMenu().get(num).getIntroduce() + "  $" + bu.getMenu().get(num).getPrice());
            System.out.println("---------------------------------------");
            System.out.println("0-退出 | 1-修改菜名 | 2-修改简介 | 3-修改价格 | 4-删除菜品");
            switch (Scanners.getChoice()){
                case 0:{
                    return;
                }
               /**修改菜名*/
                case 1:{
                    changeMenuName(bu,num);
                    break;
                }
                /**修改简介*/
                case 2:{
                    changMenuIntroduce(bu,num);
                    break;
                }
                /**修改价格*/
                case 3:{
                    changMenuPrice(bu,num);
                    break;
                }
                /**删除菜品*/
                case 4:{
                    BusSave.deleteMenu(bu.getMenu().remove(num));
                    System.out.println("删除成功");
                    Scanners.pause();
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**修改菜名*/
    public static void changeMenuName(Business bu,int num){
        while(true){
            Screens.clear();
            System.out.println("---------------------------------------");
            System.out.println("请输入新的菜名（不超过10字）");
            String newName=Scanners.getScanner();
            System.out.println("新菜名是否为"+newName);
            System.out.println("1-是 | 2-否 | 3-放弃修改");
            switch (Scanners.getChoice()){
                case 1:{
                    bu.getMenu().get(num).setName(newName);
                    BusSave.changMenuName(bu,num);
                    return;
                }
                case 2:{
                    break;
                }
                case 3:{
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }

        }
    }

    /**修改菜品介绍*/
    public static void changMenuIntroduce(Business bu,int num){
        while(true){
            Screens.clear();
            System.out.println("---------------------------------------");
            System.out.println("请输入新的介绍(不超过20字)");
            String newIntroduce=Scanners.getScanner();
            System.out.println("新菜名是否为"+newIntroduce);
            System.out.println("1-是 | 2-否 | 3-放弃修改");
            switch (Scanners.getChoice()){
                case 1:{
                    bu.getMenu().get(num).setIntroduce(newIntroduce);
                    BusSave.changMenuIntroduce(bu,num);
                    return;
                }
                case 2:{
                    break;
                }
                case 3:{
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**修改商品价格*/
    public static void changMenuPrice(Business bu,int num){
        while(true){
            Screens.clear();
            System.out.println("---------------------------------------");
            System.out.println("请输入新的价格(最多为两位小数)");
            double newPrice=0;
            newPrice=Scanners.getDouble();
            System.out.println("新的价格是否为"+newPrice);
            System.out.println("1-是 | 2-否 | 3-放弃修改");

            switch (Scanners.getChoice()){
                case 1:{
                    bu.getMenu().get(num).setPrice(newPrice);
                    BusSave.changMenuPrice(bu,num);
                    return;
                }
                case 2:{
                    break;
                }
                case 3:{
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**折扣主系统*/
    public static void showCoupons(Business bu){
        while(true){
            Screens.clear();
            BuScreen.showCoupons(bu);
            System.out.println("---------------------------------------");
            System.out.println("1-添加折扣 2-修改折扣 0-退出");

            switch (Scanners.getChoice()){
                case 0:{
                    Screens.clear();
                    return;
                }
                case 1:{
                    Screens.clear();
                    addCoupons(bu);
                    break;
                }
                case 2:{
                    Screens.clear();
                    changeCoupons(bu);
                    break;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**添加新的折扣*/
    public static void addCoupons(Business bu){
        System.out.println("请输入折扣力度（如0.86）");
        double discount=Scanners.getDouble();
        System.out.println("请输入折扣数量");
        int mount=Scanners.getChoice();
        bu.setCoupons(new Coupons(mount,discount));
        BusSave.addCoupons(bu);
        BusLoad.getNewCouponsId(bu);

    }

    /**修改折扣*/
    public static void changeCoupons(Business bu) {
        while (true) {
            Screens.clear();
            BuScreen.showCoupons(bu);
            System.out.println("---------------------------------------");
            System.out.println("0-退出 |  输入需要修改的菜单");
            int choice = 0;

            /**防止用户输入不存在的订单*/
            do {
                choice = Scanners.getChoice();
                if (choice-2 > bu.getOrders().size()) {
                    System.out.println("请输入存在的订单");
                    Scanners.pause();
                    Screens.clear();
                    BuScreen.showCoupons(bu);
                }
            } while (choice-2 > bu.getOrders().size());
            switch (choice) {
                case 0: return;
                default: {
                    changeCoupon(bu, choice - 1);
                    break;
                }
            }
        }
    }
    /**对某一折扣修改*/
    public static void changeCoupon(Business bu,int num){
        while (true) {
            System.out.println();
            System.out.println(bu.getCoupons().get(num).getDiscount()+" "+bu.getCoupons().get(num).getMount()+"份");
            System.out.println("---------------------------------------");
            System.out.println("0-退出 | 1-修改折扣 | 2-修改数量 ");
            switch (Scanners.getChoice()){
                case 0:{
                    return;
                }
                /**修改折扣*/
                case 1:{
                    changeDiscount(bu,num);
                    break;
                }
                /**修改数量*/
                case 2:{
                    changeCouponMount(bu,num);
                    break;
                }

                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**修改折扣程度*/
    public static void changeDiscount(Business bu,int num){
        while(true){
            Screens.clear();
            System.out.println("---------------------------------------");
            System.out.println("请输入新的折扣 如：0.85");
            double newDiscount=Scanners.getDouble();
            System.out.println("新菜名是否为"+newDiscount);
            System.out.println("1-是 | 2-否 | 3-放弃修改");
            switch (Scanners.getChoice()){
                case 1:{
                    bu.getCoupons().get(num).setDiscount(newDiscount);
                    BusSave.changeCouponDiscount(bu,num);
                    return;
                }
                case 2:{
                    break;
                }
                case 3:{
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**修改折扣数量*/
    public static void changeCouponMount(Business bu,int num){
        while(true){
            Screens.clear();
            System.out.println("---------------------------------------");
            System.out.println("请输入新的数量");
            int newMount=Scanners.getChoice();
            System.out.println("新菜名是否为"+newMount);
            System.out.println("1-是 | 2-否 | 3-放弃修改");
            switch (Scanners.getChoice()){
                case 1:{
                    bu.getCoupons().get(num).setMount(newMount);
                    BusSave.changeCouponMount(bu,num);
                    return;
                }
                case 2:{
                    break;
                }
                case 3:{
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

}
